package com.wwwarehouse.usercenter.fragment.businessunit;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.GridView;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.common.Common;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.adapter.BusinessInfoAdapter;
import com.wwwarehouse.usercenter.bean.BusinessInfo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Creator by zhenwang.xiang on 2017/9/26 16:22
 * Description:信息卡片viewpager中push进来的fragment
 */

public class BusunessInfoFragment extends BaseFragment {
    private GridView mGridView;
    private StateLayout mStateLayout;
    private List<BusinessInfo> mData;
    private List<BusinessInfo> mItemData;
    private int mPage;
    private String mTitle;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_busuness_info, container, false);
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        if (getArguments() != null) {
            mData = (List<BusinessInfo>) getArguments().getSerializable("infolist");
            mPage = getArguments().getInt("page");
            mTitle = getArguments().getString("title");
            if (mData != null) {
                mStateLayout.showContentView();
                for (int i = mPage * 9; i < (mPage + 1) * 9; i++) {
                    if (i >= 0 && i < mData.size()) {
                        mItemData.add(mData.get(i));
                    }
                }
                mGridView.setAdapter(new BusinessInfoAdapter(mItemData, getActivity()));

                if (mData.size() == 1) {
                    BusinessUnitInfoFragment businessUnitInfoFragment = new BusinessUnitInfoFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt("businessUnitId", mData.get(0).getBuId());
                    bundle.putString("title", mTitle);
                    businessUnitInfoFragment.setArguments(bundle);
                    pushFragment(businessUnitInfoFragment);
                }

                mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if (Common.getInstance().isNotFastClick()) {
                            BusinessUnitViewPagerFragment businessUnitViewPagerFragment = new BusinessUnitViewPagerFragment();
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("griddata", (Serializable) mData);
                            int selectposition = (mPage * 9) + position;
                            bundle.putInt("position", selectposition);
                            bundle.putString("title", mTitle);
                            businessUnitViewPagerFragment.setArguments(bundle);
                            pushFragment(businessUnitViewPagerFragment);

                        }
                    }
                });
            } else {
                mStateLayout.showEmptyView(true);
            }
        }
    }

    private void initView() {
        mGridView = $(R.id.user_gridview);
        mStateLayout = $(R.id.gridview_state);
        mStateLayout.showProgressView(true);
        mData = new ArrayList<>();
        mItemData = new ArrayList<>();
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof BusunessInfoFragment) {
            mActivity.setTitle(mTitle);
        }
    }

    @Override
    public void requestDatas() {

    }
}
